﻿using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Elasticsearch.Net;
using FortuneUniverse;
using FortuneUniverse.Fortune.Dyson;

namespace GenData
{
    public interface IGenDataJob<T>
    {
        T execute();
    }

    public class GenDataJob : IGenDataJob<List<object>>
    {
        public JobContext jobContext { get; set; }

        public int start => jobContext.seed;

        public int step => jobContext.step;

        public int end => jobContext.seed + step;

        public List<object> execute()
        {
            List<object> galaxyDataList = new List<object>(step);
            ParallelLoopResult loopResult = Parallel.For(start, end, index =>
            {
                GameDesc gameDesc = new GameDesc();
                gameDesc.SetForNewGame(index, 64);
                Dictionary<string,object> galaxyData = FortuneUniverseGen.结果解析(UniverseGen.CreateGalaxy(gameDesc));
                lock (galaxyDataList)
                {
                    galaxyDataList.Add(new
                    {
                        index = new
                        {
                            _index = "fortune_galaxy_data_simple",
                            _id = index
                        }
                    });
                    galaxyDataList.Add(galaxyData);
                }
            });
            while (!loopResult.IsCompleted)
            {
                Thread.Sleep(100);
            }

            return galaxyDataList;
        }
    }
}